﻿define('schedulingEventsConfig',
    ['jquery', 'ko', 'scheduleTemplate', 'sele'],
 function ($, ko, scheduleTemplate, sele) {
     var configure = function() {
        /* $(".mainContainer").undelegate(".profile", "click");
         $(".mainContainer").delegate(".profile", "click", function () {
             $(".profile", ".mainContainer").removeClass("active");
             var context = ko.contextFor(this);
             var model = context.$data;
             $(this).addClass("active");
             
         });*/

        
         
         $(".mainContainer").undelegate(".btnNew", "click");
         $(".mainContainer").delegate(".btnNew", "click", function () {
             var context = ko.contextFor(this);
             var model = context.$data;
             var dlg = new MedoraDialogWindow($("#templateView"));
             dlg.save = function () {
                 model.selectedObj().save(model, dlg, model.scheduleTypes());
             };
             var content = $("#formContainer").html();
             dlg.createWithSave(800, 700, "New " + model.selectedObj().templateName, "<div id='dlg'>" + content + "</div>");
             model.selectedObj(new scheduleTemplate(null, model.activeProviderId()));
             ko.applyBindings(model.selectedObj(), $("#dlg")[0]);


             return false;
         });
         
         $(".mainContainer").undelegate(".fill", "click");
         $(".mainContainer").delegate(".fill", "click", function () {
             var context = ko.contextFor(this);
             var model = context.$root;
             var obj = context.$data;

             model.selectedObj(obj);
             model.selectedObj().loadScheduleTemplateItems(obj.id(), model.scheduleTypes(), function () {
                var dlg = new MedoraDialogWindow($("#templateView"));
                dlg.save = function () {
                    model.selectedObj().save(model, dlg, model.scheduleTypes(), true);
                };
                 
                dlg.saveNotClose = function () {
                    model.selectedObj().save(model, dlg, model.scheduleTypes(), false);
                };
                var content = $("#formContainer").clone();
                dlg.createWithSaveSaveClose(800, 700, "New " + model.selectedObj().templateName, "<div id='dlg'></div>");
                 $("#dlg").append(content);
                ko.cleanNode($("#formContainer")[0]);
                 //ko.cleanNode($("#dlg")[0]);
                 ko.applyBindings(model, $("#formContainer")[0]);
                // ko.applyBindings(model.selectedObj(), $("#dlg")[0]);
                 $("#dlg").children().show();
                 //console.log();
                 $(".tt").sele();
                 $(".inputTime").regexMask("time");
             });
             
         });
         
         $(".mainContainer").undelegate(".createSchedule", "click");
         $(".mainContainer").delegate(".createSchedule", "click", function () {
             var context = ko.contextFor(this);
             var model = context.$root;
             var obj = context.$data;

             var dlg = new MedoraDialogWindow($("#templateView"));
             dlg.save = function() {
                 alert("Save");
             };

                 dlg.saveNotClose = function () {
                     alert("SaveNotClose");
                 };
                 var content = $("#scheduleCreateModal").clone();
                 dlg.createWithSaveSaveClose(800, 700, "Create Schedule", "<div id='dlg'></div>");
                 $("#dlg").append(content);
                 //ko.cleanNode($("#scheduleCreateModal")[0]);
                
                // ko.applyBindings(model, $("#formContainer")[0]);
                
                 $("#dlg").children().show();
                
                
             

         });
         
         $(".mainContainer").undelegate(".btnDelete", "click");
         $(".mainContainer").delegate(".btnDelete", "click", function () {
             var context = ko.contextFor(this);
             var model = context.$root;
             var deleteController = model.selectedObj().deleteController;
             var ids = new Array();
             $.each(model.scheduleTemplates(), function() {
                 if(this.forDelete()==true) ids.push(this.id());
             });

             
             if (ids.length > 0) {
                 //todo modify for confirm window
                 MedoraConfirm($("#templateView"), "Confirm delete", "Are you sure?",
                     function (dlg) {
                         DeleteAjax(deleteController + JSON.stringify(ids), {}, function () {
                             model.RefreshGrid();
                             dlg.close();
                             toastr.success("Deleted");

                         });
                     });
             }

         });

     };

     return {
         configure: configure
     };
 });